home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / a_utils / _archvrs / dos / source / aspitape / getdate.y < prev    next >
Encoding:
Lex Description  |  1992-01-26  |  21.8 KB  |  897 lines

  1. %{
  2. /* $Revision: 2.1 $
  3. **
  4. **  Originally written by Steven M. Bellovin <smb@research.att.com> while
  5. **  at the University of North Carolina at Chapel Hill.  Later tweaked by
  6. **  a couple of people on Usenet.  Completely overhauled by Rich $alz
  7. **  <rsalz@bbn.com> and Jim Berets <jberets@bbn.com> in August, 1990;
  8. **  send any email to Rich.
  9. **
  10. **  This grammar has eight shift/reduce conflicts.
  11. **
  12. **  This code is in the public domain and has no copyright.
  13. */
  14. /* SUPPRESS 287 on yaccpar_sccsid *//* Unusd static variable */
  15. /* SUPPRESS 288 on yyerrlab *//* Label unused */
  16.  
  17. #ifdef __GNUC__
  18. #define alloca __builtin_alloca
  19. #else
  20. #ifdef sparc
  21. #include <alloca.h>
  22. #else
  23. #ifdef _AIX /* for Bison */
  24. #pragma alloca
  25. #else
  26. char *alloca ();
  27. #endif
  28. #endif
  29. #endif
  30.  
  31. #include <stdio.h>
  32. #include <ctype.h>
  33.  
  34. #if    defined(vms)
  35. #include <types.h>
  36. #include <time.h>
  37. #else
  38. #include <sys/types.h>
  39. #if    defined(USG)
  40. /*
  41. **  Uncomment the next line if you need to do a tzset() call to set the
  42. **  timezone, and don't have ftime().  Some SystemV releases, I think.
  43. */
  44. /*#define NEED_TZSET */
  45. struct timeb {
  46.     time_t        time;        /* Seconds since the epoch    */
  47.     unsigned short    millitm;    /* Field not used        */
  48.     short        timezone;
  49.     short        dstflag;    /* Field not used        */
  50. };
  51. #else
  52. #include <sys/timeb.h>
  53. #endif    /* defined(USG) */
  54. #if    defined(BSD4_2) || defined(BSD4_1C)
  55. #include <sys/time.h>
  56. #else
  57. #include <time.h>
  58. #endif    /* defined(BSD4_2) */
  59. #endif    /* defined(vms) */
  60.  
  61. #if defined (STDC_HEADERS) || defined (USG)
  62. #include <string.h>
  63. #endif
  64.  
  65. extern struct tm    *localtime();
  66.  
  67. #define yyparse getdate_yyparse
  68. #define yylex getdate_yylex
  69. #define yyerror getdate_yyerror
  70.  
  71. #if    !defined(lint) && !defined(SABER)
  72. static char RCS[] =
  73.     "$Header: str2date.y,v 2.1 90/09/06 08:15:06 cronan Exp $";
  74. #endif    /* !defined(lint) && !defined(SABER) */
  75.  
  76.  
  77. #define EPOCH        1970
  78. #define HOUR(x)        (x * 60)
  79. #define SECSPERDAY    (24L * 60L * 60L)
  80.  
  81.  
  82. /*
  83. **  An entry in the lexical lookup table.
  84. */
  85. typedef struct _TABLE {
  86.     char    *name;
  87.     int        type;
  88.     time_t    value;
  89. } TABLE;
  90.  
  91.  
  92. /*
  93. **  Daylight-savings mode:  on, off, or not yet known.
  94. */
  95. typedef enum _DSTMODE {
  96.     DSTon, DSToff, DSTmaybe
  97. } DSTMODE;
  98.  
  99. /*
  100. **  Meridian:  am, pm, or 24-hour style.
  101. */
  102. typedef enum _MERIDIAN {
  103.     MERam, MERpm, MER24
  104. } MERIDIAN;
  105.  
  106.  
  107. /*
  108. **  Global variables.  We could get rid of most of these by using a good
  109. **  union as the yacc stack.  (This routine was originally written before
  110. **  yacc had the %union construct.)  Maybe someday; right now we only use
  111. **  the %union very rarely.
  112. */
  113. static char    *yyInput;
  114. static DSTMODE    yyDSTmode;
  115. static time_t    yyDayOrdinal;
  116. static time_t    yyDayNumber;
  117. static int    yyHaveDate;
  118. static int    yyHaveDay;
  119. static int    yyHaveRel;
  120. static int    yyHaveTime;
  121. static int    yyHaveZone;
  122. static time_t    yyTimezone;
  123. static time_t    yyDay;
  124. static time_t    yyHour;
  125. static time_t    yyMinutes;
  126. static time_t    yyMonth;
  127. static time_t    yySeconds;
  128. static time_t    yyYear;
  129. static MERIDIAN    yyMeridian;
  130. static time_t    yyRelMonth;
  131. static time_t    yyRelSeconds;
  132.  
  133. %}
  134.  
  135. %union {
  136.     time_t        Number;
  137.     enum _MERIDIAN    Meridian;
  138. }
  139.  
  140. %token    tAGO tDAY tDAYZONE tID tMERIDIAN tMINUTE_UNIT tMONTH tMONTH_UNIT
  141. %token    tSEC_UNIT tSNUMBER tUNUMBER tZONE
  142.  
  143. %type    <Number>    tDAY tDAYZONE tMINUTE_UNIT tMONTH tMONTH_UNIT
  144. %type    <Number>    tSEC_UNIT tSNUMBER tUNUMBER tZONE
  145. %type    <Meridian>    tMERIDIAN o_merid
  146.  
  147. %%
  148.  
  149. spec    : /* NULL */
  150.     | spec item
  151.     ;
  152.  
  153. item    : time {
  154.         yyHaveTime++;
  155.     }
  156.     | zone {
  157.         yyHaveZone++;
  158.     }
  159.     | date {
  160.         yyHaveDate++;
  161.     }
  162.     | day {
  163.         yyHaveDay++;
  164.     }
  165.     | rel {
  166.         yyHaveRel++;
  167.     }
  168.     | number
  169.     ;
  170.  
  171. time    : tUNUMBER tMERIDIAN {
  172.         yyHour = $1;
  173.         yyMinutes = 0;
  174.         yySeconds = 0;
  175.         yyMeridian = $2;
  176.     }
  177.     | tUNUMBER ':' tUNUMBER o_merid {
  178.         yyHour = $1;
  179.         yyMinutes = $3;
  180.         yySeconds = 0;
  181.         yyMeridian = $4;
  182.     }
  183.     | tUNUMBER ':' tUNUMBER tSNUMBER {
  184.         yyHour = $1;
  185.         yyMinutes = $3;
  186.         yyMeridian = MER24;
  187.         yyDSTmode = DSToff;
  188.         yyTimezone = - ($4 % 100 + ($4 / 100) * 60);
  189.     }
  190.     | tUNUMBER ':' tUNUMBER ':' tUNUMBER o_merid {
  191.         yyHour = $1;
  192.         yyMinutes = $3;
  193.         yySeconds = $5;
  194.         yyMeridian = $6;
  195.     }
  196.     | tUNUMBER ':' tUNUMBER ':' tUNUMBER tSNUMBER {
  197.         yyHour = $1;
  198.         yyMinutes = $3;
  199.         yySeconds = $5;
  200.         yyMeridian = MER24;
  201.         yyDSTmode = DSToff;
  202.         yyTimezone = - ($6 % 100 + ($6 / 100) * 60);
  203.     }
  204.     ;
  205.  
  206. zone    : tZONE {
  207.         yyTimezone = $1;
  208.         yyDSTmode = DSToff;
  209.     }
  210.     | tDAYZONE {
  211.         yyTimezone = $1;
  212.         yyDSTmode = DSTon;
  213.     }
  214.     ;
  215.  
  216. day    : tDAY {
  217.         yyDayOrdinal = 1;
  218.         yyDayNumber = $1;
  219.     }
  220.     | tDAY ',' {
  221.         yyDayOrdinal = 1;
  222.         yyDayNumber = $1;
  223.     }
  224.     | tUNUMBER tDAY {
  225.         yyDayOrdinal = $1;
  226.         yyDayNumber = $2;
  227.     }
  228.     ;
  229.  
  230. date    : tUNUMBER '/' tUNUMBER {
  231.         yyMonth = $1;
  232.         yyDay = $3;
  233.     }
  234.     | tUNUMBER '/' tUNUMBER '/' tUNUMBER {
  235.         yyMonth = $1;
  236.         yyDay = $3;
  237.         yyYear = $5;
  238.     }
  239.     | tMONTH tUNUMBER {
  240.         yyMonth = $1;
  241.         yyDay = $2;
  242.     }
  243.     | tMONTH tUNUMBER ',' tUNUMBER {
  244.         yyMonth = $1;
  245.         yyDay = $2;
  246.         yyYear = $4;
  247.     }
  248.     | tUNUMBER tMONTH {
  249.         yyMonth = $2;
  250.         yyDay = $1;
  251.     }
  252.     | tUNUMBER tMONTH tUNUMBER {
  253.         yyMonth = $2;
  254.         yyDay = $1;
  255.         yyYear = $3;
  256.     }
  257.     ;
  258.  
  259. rel    : relunit tAGO {
  260.         yyRelSeconds = -yyRelSeconds;
  261.         yyRelMonth = -yyRelMonth;
  262.     }
  263.     | relunit
  264.     ;
  265.  
  266. relunit    : tUNUMBER tMINUTE_UNIT {
  267.         yyRelSeconds += $1 * $2 * 60L;
  268.     }
  269.     | tSNUMBER tMINUTE_UNIT {
  270.         yyRelSeconds += $1 * $2 * 60L;
  271.     }
  272.     | tMINUTE_UNIT {
  273.         yyRelSeconds += $1 * 60L;
  274.     }
  275.     | tSNUMBER tSEC_UNIT {
  276.         yyRelSeconds += $1;
  277.     }
  278.     | tUNUMBER tSEC_UNIT {
  279.         yyRelSeconds += $1;
  280.     }
  281.     | tSEC_UNIT {
  282.         yyRelSeconds++;
  283.     }
  284.     | tSNUMBER tMONTH_UNIT {
  285.         yyRelMonth += $1 * $2;
  286.     }
  287.     | tUNUMBER tMONTH_UNIT {
  288.         yyRelMonth += $1 * $2;
  289.     }
  290.     | tMONTH_UNIT {
  291.         yyRelMonth += $1;
  292.     }
  293.     ;
  294.  
  295. number    : tUNUMBER {
  296.         if (yyHaveTime && yyHaveDate && !yyHaveRel)
  297.         yyYear = $1;
  298.         else {
  299.         if($1>10000) {
  300.             time_t date_part;
  301.  
  302.             date_part= $1/10000;
  303.             yyHaveDate++;
  304.             yyDay= (date_part)%100;
  305.             yyMonth= (date_part/100)%100;
  306.             yyYear = date_part/10000;
  307.         } 
  308.             yyHaveTime++;
  309.         if ($1 < 100) {
  310.             yyHour = $1;
  311.             yyMinutes = 0;
  312.         }
  313.         else {
  314.             yyHour = $1 / 100;
  315.             yyMinutes = $1 % 100;
  316.         }
  317.         yySeconds = 0;
  318.         yyMeridian = MER24;
  319.         }
  320.     }
  321.     ;
  322.  
  323. o_merid    : /* NULL */ {
  324.         $$ = MER24;
  325.     }
  326.     | tMERIDIAN {
  327.         $$ = $1;
  328.     }
  329.     ;
  330.  
  331. %%
  332.  
  333. /* Month and day table. */
  334. static TABLE    MonthDayTable[] = {
  335.     { "january",    tMONTH,  1 },
  336.     { "february",    tMONTH,  2 },
  337.     { "march",        tMONTH,  3 },
  338.     { "april",        tMONTH,  4 },
  339.     { "may",        tMONTH,  5 },
  340.     { "june",        tMONTH,  6 },
  341.     { "july",        tMONTH,  7 },
  342.     { "august",        tMONTH,  8 },
  343.     { "september",    tMONTH,  9 },
  344.     { "sept",        tMONTH,  9 },
  345.     { "october",    tMONTH, 10 },
  346.     { "november",    tMONTH, 11 },
  347.     { "december",    tMONTH, 12 },
  348.     { "sunday",        tDAY, 0 },
  349.     { "monday",        tDAY, 1 },
  350.     { "tuesday",    tDAY, 2 },
  351.     { "tues",        tDAY, 2 },
  352.     { "wednesday",    tDAY, 3 },
  353.     { "wednes",        tDAY, 3 },
  354.     { "thursday",    tDAY, 4 },
  355.     { "thur",        tDAY, 4 },
  356.     { "thurs",        tDAY, 4 },
  357.     { "friday",        tDAY, 5 },
  358.     { "saturday",    tDAY, 6 },
  359.     { NULL }
  360. };
  361.  
  362. /* Time units table. */
  363. static TABLE    UnitsTable[] = {
  364.     { "year",        tMONTH_UNIT,    12 },
  365.     { "month",        tMONTH_UNIT,    1 },
  366.     { "fortnight",    tMINUTE_UNIT,    14 * 24 * 60 },
  367.     { "week",        tMINUTE_UNIT,    7 * 24 * 60 },
  368.     { "day",        tMINUTE_UNIT,    1 * 24 * 60 },
  369.     { "hour",        tMINUTE_UNIT,    60 },
  370.     { "minute",        tMINUTE_UNIT,    1 },
  371.     { "min",        tMINUTE_UNIT,    1 },
  372.     { "second",        tSEC_UNIT,    1 },
  373.     { "sec",        tSEC_UNIT,    1 },
  374.     { NULL }
  375. };
  376.  
  377. /* Assorted relative-time words. */
  378. static TABLE    OtherTable[] = {
  379.     { "tomorrow",    tMINUTE_UNIT,    1 * 24 * 60 },
  380.     { "yesterday",    tMINUTE_UNIT,    -1 * 24 * 60 },
  381.     { "today",        tMINUTE_UNIT,    0 },
  382.     { "now",        tMINUTE_UNIT,    0 },
  383.     { "last",        tUNUMBER,    -1 },
  384.     { "this",        tMINUTE_UNIT,    0 },
  385.     { "next",        tUNUMBER,    2 },
  386.     { "first",        tUNUMBER,    1 },
  387. /*  { "second",        tUNUMBER,    2 }, */
  388.     { "third",        tUNUMBER,    3 },
  389.     { "fourth",        tUNUMBER,    4 },
  390.     { "fifth",        tUNUMBER,    5 },
  391.     { "sixth",        tUNUMBER,    6 },
  392.     { "seventh",    tUNUMBER,    7 },
  393.     { "eighth",        tUNUMBER,    8 },
  394.     { "ninth",        tUNUMBER,    9 },
  395.     { "tenth",        tUNUMBER,    10 },
  396.     { "eleventh",    tUNUMBER,    11 },
  397.     { "twelfth",    tUNUMBER,    12 },
  398.     { "ago",        tAGO,    1 },
  399.     { NULL }
  400. };
  401.  
  402. /* The timezone table. */
  403. static TABLE    TimezoneTable[] = {
  404.     { "gmt",    tZONE,     HOUR( 0) },    /* Greenwich Mean */
  405.     { "ut",    tZONE,     HOUR( 0) },    /* Universal (Coordinated) */
  406.     { "utc",    tZONE,     HOUR( 0) },
  407.     { "wet",    tZONE,     HOUR( 0) },    /* Western European */
  408.     { "bst",    tDAYZONE,  HOUR( 0) },    /* British Summer */
  409.     { "wat",    tZONE,     HOUR( 1) },    /* West Africa */
  410.     { "at",    tZONE,     HOUR( 2) },    /* Azores */
  411. #if    0
  412.     /* For completeness.  BST is also British Summer, and GST is
  413.      * also Guam Standard. */
  414.     { "bst",    tZONE,     HOUR( 3) },    /* Brazil Standard */
  415.     { "gst",    tZONE,     HOUR( 3) },    /* Greenland Standard */
  416. #endif
  417.     { "nft",    tZONE,     HOUR(3.5) },    /* Newfoundland */
  418.     { "nst",    tZONE,     HOUR(3.5) },    /* Newfoundland Standard */
  419.     { "ndt",    tDAYZONE,  HOUR(3.5) },    /* Newfoundland Daylight */
  420.     { "ast",    tZONE,     HOUR( 4) },    /* Atlantic Standard */
  421.     { "adt",    tDAYZONE,  HOUR( 4) },    /* Atlantic Daylight */
  422.     { "est",    tZONE,     HOUR( 5) },    /* Eastern Standard */
  423.     { "edt",    tDAYZONE,  HOUR( 5) },    /* Eastern Daylight */
  424.     { "cst",    tZONE,     HOUR( 6) },    /* Central Standard */
  425.     { "cdt",    tDAYZONE,  HOUR( 6) },    /* Central Daylight */
  426.     { "mst",    tZONE,     HOUR( 7) },    /* Mountain Standard */
  427.     { "mdt",    tDAYZONE,  HOUR( 7) },    /* Mountain Daylight */
  428.     { "pst",    tZONE,     HOUR( 8) },    /* Pacific Standard */
  429.     { "pdt",    tDAYZONE,  HOUR( 8) },    /* Pacific Daylight */
  430.     { "yst",    tZONE,     HOUR( 9) },    /* Yukon Standard */
  431.     { "ydt",    tDAYZONE,  HOUR( 9) },    /* Yukon Daylight */
  432.     { "hst",    tZONE,     HOUR(10) },    /* Hawaii Standard */
  433.     { "hdt",    tDAYZONE,  HOUR(10) },    /* Hawaii Daylight */
  434.     { "cat",    tZONE,     HOUR(10) },    /* Central Alaska */
  435.     { "ahst",    tZONE,     HOUR(10) },    /* Alaska-Hawaii Standard */
  436.     { "nt",    tZONE,     HOUR(11) },    /* Nome */
  437.     { "idlw",    tZONE,     HOUR(12) },    /* International Date Line West */
  438.     { "cet",    tZONE,     -HOUR(1) },    /* Central European */
  439.     { "met",    tZONE,     -HOUR(1) },    /* Middle European */
  440.     { "mewt",    tZONE,     -HOUR(1) },    /* Middle European Winter */
  441.     { "mest",    tDAYZONE,  -HOUR(1) },    /* Middle European Summer */
  442.     { "swt",    tZONE,     -HOUR(1) },    /* Swedish Winter */
  443.     { "sst",    tDAYZONE,  -HOUR(1) },    /* Swedish Summer */
  444.     { "fwt",    tZONE,     -HOUR(1) },    /* French Winter */
  445.     { "fst",    tDAYZONE,  -HOUR(1) },    /* French Summer */
  446.     { "eet",    tZONE,     -HOUR(2) },    /* Eastern Europe, USSR Zone 1 */
  447.     { "bt",    tZONE,     -HOUR(3) },    /* Baghdad, USSR Zone 2 */
  448.     { "it",    tZONE,     -HOUR(3.5) },/* Iran */
  449.     { "zp4",    tZONE,     -HOUR(4) },    /* USSR Zone 3 */
  450.     { "zp5",    tZONE,     -HOUR(5) },    /* USSR Zone 4 */
  451.     { "ist",    tZONE,     -HOUR(5.5) },/* Indian Standard */
  452.     { "zp6",    tZONE,     -HOUR(6) },    /* USSR Zone 5 */
  453. #if    0
  454.     /* For completeness.  NST is also Newfoundland Stanard, nad SST is
  455.      * also Swedish Summer. */
  456.     { "nst",    tZONE,     -HOUR(6.5) },/* North Sumatra */
  457.     { "sst",    tZONE,     -HOUR(7) },    /* South Sumatra, USSR Zone 6 */
  458. #endif    /* 0 */
  459.     { "wast",    tZONE,     -HOUR(7) },    /* West Australian Standard */
  460.     { "wadt",    tDAYZONE,  -HOUR(7) },    /* West Australian Daylight */
  461.     { "jt",    tZONE,     -HOUR(7.5) },/* Java (3pm in Cronusland!) */
  462.     { "cct",    tZONE,     -HOUR(8) },    /* China Coast, USSR Zone 7 */
  463.     { "jst",    tZONE,     -HOUR(9) },    /* Japan Standard, USSR Zone 8 */
  464.     { "cast",    tZONE,     -HOUR(9.5) },/* Central Australian Standard */
  465.     { "cadt",    tDAYZONE,  -HOUR(9.5) },/* Central Australian Daylight */
  466.     { "east",    tZONE,     -HOUR(10) },    /* Eastern Australian Standard */
  467.     { "eadt",    tDAYZONE,  -HOUR(10) },    /* Eastern Australian Daylight */
  468.     { "gst",    tZONE,     -HOUR(10) },    /* Guam Standard, USSR Zone 9 */
  469.     { "nzt",    tZONE,     -HOUR(12) },    /* New Zealand */
  470.     { "nzst",    tZONE,     -HOUR(12) },    /* New Zealand Standard */
  471.     { "nzdt",    tDAYZONE,  -HOUR(12) },    /* New Zealand Daylight */
  472.     { "idle",    tZONE,     -HOUR(12) },    /* International Date Line East */
  473.     {  NULL  }
  474. };
  475.  
  476. /* Military timezone table. */
  477. static TABLE    MilitaryTable[] = {
  478.     { "a",    tZONE,    HOUR(  1) },
  479.     { "b",    tZONE,    HOUR(  2) },
  480.     { "c",    tZONE,    HOUR(  3) },
  481.     { "d",    tZONE,    HOUR(  4) },
  482.     { "e",    tZONE,    HOUR(  5) },
  483.     { "f",    tZONE,    HOUR(  6) },
  484.     { "g",    tZONE,    HOUR(  7) },
  485.     { "h",    tZONE,    HOUR(  8) },
  486.     { "i",    tZONE,    HOUR(  9) },
  487.     { "k",    tZONE,    HOUR( 10) },
  488.     { "l",    tZONE,    HOUR( 11) },
  489.     { "m",    tZONE,    HOUR( 12) },
  490.     { "n",    tZONE,    HOUR(- 1) },
  491.     { "o",    tZONE,    HOUR(- 2) },
  492.     { "p",    tZONE,    HOUR(- 3) },
  493.     { "q",    tZONE,    HOUR(- 4) },
  494.     { "r",    tZONE,    HOUR(- 5) },
  495.     { "s",    tZONE,    HOUR(- 6) },
  496.     { "t",    tZONE,    HOUR(- 7) },
  497.     { "u",    tZONE,    HOUR(- 8) },
  498.     { "v",    tZONE,    HOUR(- 9) },
  499.     { "w",    tZONE,    HOUR(-10) },
  500.     { "x",    tZONE,    HOUR(-11) },
  501.     { "y",    tZONE,    HOUR(-12) },
  502.     { "z",    tZONE,    HOUR(  0) },
  503.     { NULL }
  504. };
  505.  
  506.  
  507.  
  508.  
  509. /* ARGSUSED */
  510. int
  511. yyerror(s)
  512.     char    *s;
  513. {
  514.   return 0;
  515. }
  516.  
  517.  
  518. static time_t
  519. ToSeconds(Hours, Minutes, Seconds, Meridian)
  520.     time_t    Hours;
  521.     time_t    Minutes;
  522.     time_t    Seconds;
  523.     MERIDIAN    Meridian;
  524. {
  525.     if (Minutes < 0 || Minutes > 59 || Seconds < 0 || Seconds > 59)
  526.     return -1;
  527.     switch (Meridian) {
  528.     case MER24:
  529.     if (Hours < 0 || Hours > 23)
  530.         return -1;
  531.     return (Hours * 60L + Minutes) * 60L + Seconds;
  532.     case MERam:
  533.     if (Hours < 1 || Hours > 12)
  534.         return -1;
  535.     return (Hours * 60L + Minutes) * 60L + Seconds;
  536.     case MERpm:
  537.     if (Hours < 1 || Hours > 12)
  538.         return -1;
  539.     return ((Hours + 12) * 60L + Minutes) * 60L + Seconds;
  540.     }
  541.     /* NOTREACHED */
  542. }
  543.  
  544.  
  545. static time_t
  546. Convert(Month, Day, Year, Hours, Minutes, Seconds, Meridian, DSTmode)
  547.     time_t    Month;
  548.     time_t    Day;
  549.     time_t    Year;
  550.     time_t    Hours;
  551.     time_t    Minutes;
  552.     time_t    Seconds;
  553.     MERIDIAN    Meridian;
  554.     DSTMODE    DSTmode;
  555. {
  556.     static int    DaysInMonth[12] = {
  557.     31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  558.     };
  559.     time_t    tod;
  560.     time_t    Julian;
  561.     int        i;
  562.  
  563.     if (Year < 0)
  564.     Year = -Year;
  565.     if (Year < 100)
  566.     Year += 1900;
  567.     DaysInMonth[1] = Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0)
  568.             ? 29 : 28;
  569.     if (Year < EPOCH || Year > 1999
  570.      || Month < 1 || Month > 12
  571.      /* Lint fluff:  "conversion from long may lose accuracy" */
  572.      || Day < 1 || Day > DaysInMonth[(int)--Month])
  573.     return -1;
  574.  
  575.     for (Julian = Day - 1, i = 0; i < Month; i++)
  576.     Julian += DaysInMonth[i];
  577.     for (i = EPOCH; i < Year; i++)
  578.     Julian += 365 + (i % 4 == 0);
  579.     Julian *= SECSPERDAY;
  580.     Julian += yyTimezone * 60L;
  581.     if ((tod = ToSeconds(Hours, Minutes, Seconds, Meridian)) < 0)
  582.     return -1;
  583.     Julian += tod;
  584.     if (DSTmode == DSTon
  585.      || (DSTmode == DSTmaybe && localtime(&Julian)->tm_isdst))
  586.     Julian -= 60 * 60;
  587.     return Julian;
  588. }
  589.  
  590.  
  591. static time_t
  592. DSTcorrect(Start, Future)
  593.     time_t    Start;
  594.     time_t    Future;
  595. {
  596.     time_t    StartDay;
  597.     time_t    FutureDay;
  598.  
  599.     StartDay = (localtime(&Start)->tm_hour + 1) % 24;
  600.     FutureDay = (localtime(&Future)->tm_hour + 1) % 24;
  601.     return (Future - Start) + (StartDay - FutureDay) * 60L * 60L;
  602. }
  603.  
  604.  
  605. static time_t
  606. RelativeDate(Start, DayOrdinal, DayNumber)
  607.     time_t    Start;
  608.     time_t    DayOrdinal;
  609.     time_t    DayNumber;
  610. {
  611.     struct tm    *tm;
  612.     time_t    now;
  613.  
  614.     now = Start;
  615.     tm = localtime(&now);
  616.     now += SECSPERDAY * ((DayNumber - tm->tm_wday + 7) % 7);
  617.     now += 7 * SECSPERDAY * (DayOrdinal <= 0 ? DayOrdinal : DayOrdinal - 1);
  618.     return DSTcorrect(Start, now);
  619. }
  620.  
  621.  
  622. static time_t
  623. RelativeMonth(Start, RelMonth)
  624.     time_t    Start;
  625.     time_t    RelMonth;
  626. {
  627.     struct tm    *tm;
  628.     time_t    Month;
  629.     time_t    Year;
  630.  
  631.     if (RelMonth == 0)
  632.     return 0;
  633.     tm = localtime(&Start);
  634.     Month = 12 * tm->tm_year + tm->tm_mon + RelMonth;
  635.     Year = Month / 12;
  636.     Month = Month % 12 + 1;
  637.     return DSTcorrect(Start,
  638.         Convert(Month, (time_t)tm->tm_mday, Year,
  639.         (time_t)tm->tm_hour, (time_t)tm->tm_min, (time_t)tm->tm_sec,
  640.         MER24, DSTmaybe));
  641. }
  642.  
  643.  
  644. static int
  645. LookupWord(buff)
  646.     char        *buff;
  647. {
  648.     register char    *p;
  649.     register char    *q;
  650.     register TABLE    *tp;
  651.     int            i;
  652.     int            abbrev;
  653.  
  654.     /* Make it lowercase. */
  655.     for (p = buff; *p; p++)
  656.     if (isupper(*p))
  657.         *p = tolower(*p);
  658.  
  659.     if (strcmp(buff, "am") == 0 || strcmp(buff, "a.m.") == 0) {
  660.     yylval.Meridian = MERam;
  661.     return tMERIDIAN;
  662.     }
  663.     if (strcmp(buff, "pm") == 0 || strcmp(buff, "p.m.") == 0) {
  664.     yylval.Meridian = MERpm;
  665.     return tMERIDIAN;
  666.     }
  667.  
  668.     /* See if we have an abbreviation for a month. */
  669.     if (strlen(buff) == 3)
  670.     abbrev = 1;
  671.     else if (strlen(buff) == 4 && buff[3] == '.') {
  672.     abbrev = 1;
  673.     buff[3] = '\0';
  674.     }
  675.     else
  676.     abbrev = 0;
  677.  
  678.     for (tp = MonthDayTable; tp->name; tp++) {
  679.     if (abbrev) {
  680.         if (strncmp(buff, tp->name, 3) == 0) {
  681.         yylval.Number = tp->value;
  682.         return tp->type;
  683.         }
  684.     }
  685.     else if (strcmp(buff, tp->name) == 0) {
  686.         yylval.Number = tp->value;
  687.         return tp->type;
  688.     }
  689.     }
  690.  
  691.     for (tp = TimezoneTable; tp->name; tp++)
  692.     if (strcmp(buff, tp->name) == 0) {
  693.         yylval.Number = tp->value;
  694.         return tp->type;
  695.     }
  696.  
  697.     for (tp = UnitsTable; tp->name; tp++)
  698.     if (strcmp(buff, tp->name) == 0) {
  699.         yylval.Number = tp->value;
  700.         return tp->type;
  701.     }
  702.  
  703.     /* Strip off any plural and try the units table again. */
  704.     i = strlen(buff) - 1;
  705.     if (buff[i] == 's') {
  706.     buff[i] = '\0';
  707.     for (tp = UnitsTable; tp->name; tp++)
  708.         if (strcmp(buff, tp->name) == 0) {
  709.         yylval.Number = tp->value;
  710.         return tp->type;
  711.         }
  712.     buff[i] = 's';        /* Put back for "this" in OtherTable. */
  713.     }
  714.  
  715.     for (tp = OtherTable; tp->name; tp++)
  716.     if (strcmp(buff, tp->name) == 0) {
  717.         yylval.Number = tp->value;
  718.         return tp->type;
  719.     }
  720.  
  721.     /* Military timezones. */
  722.     if (buff[1] == '\0' && isalpha(*buff)) {
  723.     for (tp = MilitaryTable; tp->name; tp++)
  724.         if (strcmp(buff, tp->name) == 0) {
  725.         yylval.Number = tp->value;
  726.         return tp->type;
  727.         }
  728.     }
  729.  
  730.     /* Drop out any periods and try the timezone table again. */
  731.     for (i = 0, p = q = buff; *q; q++)
  732.     if (*q != '.')
  733.         *p++ = *q;
  734.     else
  735.         i++;
  736.     *p = '\0';
  737.     if (i)
  738.     for (tp = TimezoneTable; tp->name; tp++)
  739.         if (strcmp(buff, tp->name) == 0) {
  740.         yylval.Number = tp->value;
  741.         return tp->type;
  742.         }
  743.  
  744.     return tID;
  745. }
  746.  
  747.  
  748. int
  749. yylex()
  750. {
  751.     register char    c;
  752.     register char    *p;
  753.     char        buff[20];
  754.     int            Count;
  755.     int            sign;
  756.  
  757.     for ( ; ; ) {
  758.     while (isspace(*yyInput))
  759.         yyInput++;
  760.  
  761.     if (isdigit(c = *yyInput) || c == '-' || c == '+') {
  762.         if (c == '-' || c == '+') {
  763.         sign = c == '-' ? -1 : 1;
  764.         if (!isdigit(*++yyInput))
  765.             /* skip the '-' sign */
  766.             continue;
  767.         }
  768.         else
  769.         sign = 0;
  770.         for (yylval.Number = 0; isdigit(c = *yyInput++); )
  771.         yylval.Number = 10 * yylval.Number + c - '0';
  772.         yyInput--;
  773.         if (sign < 0)
  774.         yylval.Number = -yylval.Number;
  775.         return sign ? tSNUMBER : tUNUMBER;
  776.     }
  777.     if (isalpha(c)) {
  778.         for (p = buff; isalpha(c = *yyInput++) || c == '.'; )
  779.         if (p < &buff[sizeof buff - 1])
  780.             *p++ = c;
  781.         *p = '\0';
  782.         yyInput--;
  783.         return LookupWord(buff);
  784.     }
  785.     if (c != '(')
  786.         return *yyInput++;
  787.     Count = 0;
  788.     do {
  789.         c = *yyInput++;
  790.         if (c == '\0')
  791.         return c;
  792.         if (c == '(')
  793.         Count++;
  794.         else if (c == ')')
  795.         Count--;
  796.     } while (Count > 0);
  797.     }
  798. }
  799.  
  800.  
  801. time_t
  802. get_date(p, now)
  803.     char        *p;
  804.     struct timeb    *now;
  805. {
  806.     struct tm        *tm;
  807.     struct timeb    ftz;
  808.     time_t        Start;
  809.     time_t        tod;
  810.  
  811.     yyInput = p;
  812.     if (now == NULL) {
  813.     now = &ftz;
  814. #if    defined(NEED_TZSET)
  815.     (void)time(&ftz.time);
  816.     /* Set the timezone global. */
  817.     tzset();
  818.     ftz.timezone = (int) timezone / 60;
  819. #else
  820.     (void)ftime(&ftz);
  821. #endif    /* defined(NEED_TZSET) */
  822.     }
  823.  
  824.     tm = localtime(&now->time);
  825.     yyYear = tm->tm_year;
  826.     yyMonth = tm->tm_mon + 1;
  827.     yyDay = tm->tm_mday;
  828.     yyTimezone = now->timezone;
  829.     yyDSTmode = DSTmaybe;
  830.     yyHour = 0;
  831.     yyMinutes = 0;
  832.     yySeconds = 0;
  833.     yyMeridian = MER24;
  834.     yyRelSeconds = 0;
  835.     yyRelMonth = 0;
  836.     yyHaveDate = 0;
  837.     yyHaveDay = 0;
  838.     yyHaveRel = 0;
  839.     yyHaveTime = 0;
  840.     yyHaveZone = 0;
  841.  
  842.     if (yyparse()
  843.      || yyHaveTime > 1 || yyHaveZone > 1 || yyHaveDate > 1 || yyHaveDay > 1)
  844.     return -1;
  845.  
  846.     if (yyHaveDate || yyHaveTime || yyHaveDay) {
  847.     Start = Convert(yyMonth, yyDay, yyYear, yyHour, yyMinutes, yySeconds,
  848.             yyMeridian, yyDSTmode);
  849.     if (Start < 0)
  850.         return -1;
  851.     }
  852.     else {
  853.     Start = now->time;
  854.     if (!yyHaveRel)
  855.         Start -= ((tm->tm_hour * 60L + tm->tm_min) * 60L) + tm->tm_sec;
  856.     }
  857.  
  858.     Start += yyRelSeconds;
  859.     Start += RelativeMonth(Start, yyRelMonth);
  860.  
  861.     if (yyHaveDay && !yyHaveDate) {
  862.     tod = RelativeDate(Start, yyDayOrdinal, yyDayNumber);
  863.     Start += tod;
  864.     }
  865.  
  866.     /* Have to do *something* with a legitimate -1 so it's distinguishable
  867.      * from the error return value.  (Alternately could set errno on error.) */
  868.     return Start == -1 ? 0 : Start;
  869. }
  870.  
  871.  
  872. #if    defined(TEST)
  873.  
  874. /* ARGSUSED */
  875. main(ac, av)
  876.     int        ac;
  877.     char    *av[];
  878. {
  879.     char    buff[128];
  880.     time_t    d;
  881.  
  882.     (void)printf("Enter date, or blank line to exit.\n\t> ");
  883.     (void)fflush(stdout);
  884.     while (gets(buff) && buff[0]) {
  885.     d = get_date(buff, (struct timeb *)NULL);
  886.     if (d == -1)
  887.         (void)printf("Bad format - couldn't convert.\n");
  888.     else
  889.         (void)printf("%s", ctime(&d));
  890.     (void)printf("\t> ");
  891.     (void)fflush(stdout);
  892.     }
  893.     exit(0);
  894.     /* NOTREACHED */
  895. }
  896. #endif    /* defined(TEST) */
  897.